<?php

if (!defined('NV_SYSTEM')) { die ("You can't access this file directly..."); }

require_once("mainfile.php");
$module_name = basename(dirname(__FILE__));
get_lang($module_name);
if (file_exists("".$datafold."/config_".$module_name.".php")) {
	@require_once ("".$datafold."/config_".$module_name.".php");
}
if (defined('_MODTITLE')) $module_title = _MODTITLE;
$index = 1;
#############################################
# 	Nukeviet Voting - 05/07/06          # 
#############################################

//Hien thi ket qua tham do` = Cap nhat gia tri binh` chon vao CSDL

function xemketqua($pollid) {
	global $db, $prefix, $multilingual, $currentlang, $module_name, $client_ip;
	$pollid = intval($pollid);
	include('header.php');
	if ($multilingual == 1) { $querylang = "AND planguage='$currentlang'"; } else { $querylang = ""; }
	$sql = "SELECT * FROM ".$prefix."_nvvotings WHERE pollid='$pollid' $querylang";
	$result = $db->sql_query($sql);
	$row = $db->sql_fetchrow($result);
	$totalevote = explode("|",$row['votes']);
	$option = explode("|",$row['optiontext']);
	$total = intval($row['totalvotes']);
	OpenTable();
		echo"<table border=\"0\" cellpadding=\"2\" width=\"100%\" cellspacing=\"1\">\n";
		echo "<tr>\n";
		echo "<td width=\"100%\" colspan=\"2\">"._KETQUATHAMDO." : <b>".stripslashes(FixQuotes($row['question']))."</b></td>\n";
		echo "</tr>\n";
	if($total > 0) {
		$options = '';
		for($i=0;$i < intval($row['options']);$i++) {
			$totalevote[$i] = intval($totalevote[$i]);
			$eachopt[$i] = round(($totalevote[$i]/$total)*100);
			$phantram[$i] = "".round(($totalevote[$i]/$total)*100, 2)."%";
			echo "<tr>\n";
			echo "<td width=\"50%\"><b>".$option[$i]."</b>&nbsp;(".$totalevote[$i]." "._PHIEU." -  ".$phantram[$i].")</td>\n";
			echo "<td width=\"50%\">\n";
			echo "<table border=\"0\" cellpadding=\"0\" style=\"border-collapse: collapse\" width=\"100%\">\n";
			echo "<tr>\n";
			if($eachopt[$i]!=0) {
			echo "<td>\n";
			echo "<img border=\"0\" src=\"images/spacer.gif\" width=\"1\" height=\"1\"></td>\n";
			echo "<td background=\"images/modules/$module_name/bar_middle.gif\" width=\"".$eachopt[$i]."%\">\n";
			echo "<img border=\"0\" src=\"images/modules/$module_name/bar_middle.gif\" width=\"1\" height=\"10\"></td>\n";
			} else {
			echo "<td colspan=\"2\">\n";
			echo "<img border=\"0\" src=\"images/spacer.gif\" width=\"1\" height=\"1\"></td>\n";
			}
			echo "</tr>\n";
			echo "</table>\n";
			echo "</td>\n";
			echo "</tr>\n";
		}
	}
	echo "<tr>\n";
	echo "<td width=\"100%\" colspan=\"2\" align=\"right\"><b>"._TOTALVOTES."</b>&nbsp;<font color='brown'><b>".$total."</b></font></td>\n";
	echo "</tr>\n";
	echo "</table>\n";
	CloseTable();
	echo "<br>";
	OpenTable();
	echo "<center>[ <b>&raquo; <a href=\"modules.php?name=$module_name&op=viewpoll&pollid=$pollid\">"._VOTING."</a> &raquo; <a href=\"modules.php?name=$module_name\">"._POLLS."</a></b> ]</center>";
	CloseTable();
	if ($row['acomm'] != 0) {
		echo "<br>";
		OpenTable();
		include("modules/$module_name/comments.php");
		CloseTable();
	}
	include('footer.php');	
	exit();
}

function pollvote(){
	global $db, $prefix, $multilingual, $currentlang, $module_name, $client_ip;
	$pollid = intval(( isset($_POST['pollid']) ) ? $_POST['pollid'] : $_GET['pollid']);
	if (!isset($pollid)) { Header("Location: index.php"); exit(); }
	if($db->sql_numrows($db->sql_query("SELECT * FROM ".$prefix."_nvvotings WHERE pollid='$pollid'")) == 0) { Header("Location: index.php"); exit; }
	if(isset($_POST['option_id']) AND is_numeric($_POST['option_id'])) {
		
		$option_id = intval($_POST['option_id']);
		$past = time()-86400;
		$db->sql_query("DELETE FROM ".$prefix."_nvvoting_votes WHERE vottime < $past");
		if($db->sql_numrows($db->sql_query("SELECT * FROM ".$prefix."_nvvoting_votes WHERE pollid='$pollid' AND ip='$client_ip'")) == 0) {
			if ($multilingual == 1) { $querylang = "AND planguage='$currentlang'"; } else { $querylang = ""; }
			$sql = "SELECT * FROM ".$prefix."_nvvotings WHERE pollid='$pollid' $querylang";
			$result = $db->sql_query($sql);
			$row = $db->sql_fetchrow($result);
			$totalevote = explode("|",$row['votes']);
			$newvotes = array();
			for($i=0;$i < intval($row['options']);$i++) {
				$newvotes[$i] = intval($totalevote[$i]);
				if($i==$option_id) {
					$newvotes[$i] = intval($totalevote[$i]) + 1;
				}
			}
			$newvotes = implode("|",$newvotes);
			$db->sql_query("UPDATE ".$prefix."_nvvotings SET votes = '$newvotes', totalvotes = totalvotes+1 WHERE pollid = ".$pollid."");
			$db->sql_query("INSERT INTO ".$prefix."_nvvoting_votes (ip, vottime, pollid) VALUES ('$client_ip', '".time()."', '$pollid')");
			Header("Location: modules.php?name=$module_name&op=pollvote&pollid=$pollid");
			exit;
		} else {
			xemketqua($pollid);
		exit();
		}
	} else {
		xemketqua($pollid);
		exit();
	}
}

//Giao dien xem va binh chon 1 tham do
function viewpoll(){
global $db, $prefix, $multilingual, $currentlang, $module_name;
	$pollid = intval(( isset($_POST['pollid']) ) ? $_POST['pollid'] : $_GET['pollid']);
 	if (!isset($pollid)) { Header("Location: index.php"); exit(); }
	if ($db->sql_numrows($db->sql_query("SELECT * FROM ".$prefix."_nvvotings WHERE pollid='$pollid'")) != 1) { Header("Location: index.php"); exit; }
	include('header.php');
	OpenTable();
	if ($multilingual == 1) { $querylang = "AND planguage='$currentlang'"; } else { $querylang = ""; }
	$sql = "SELECT * FROM ".$prefix."_nvvotings WHERE pollid='$pollid' $querylang";
	$result = $db->sql_query($sql);
	$row = $db->sql_fetchrow($result);
	$pollid = intval($row['pollid']);
	$question = stripslashes(FixQuotes($row['question']));
	$option = explode("|",$row['optiontext']);
	$options = intval($row['options']);
	$acomm = intval($row['acomm']);
	$totalvotes = intval($row['totalvotes']);
	$totalcomm = intval($row['totalcomm']);
	echo "<form action=\"modules.php?name=".$module_name."\" method=\"post\">\n";
	echo "<input type=\"hidden\" name=\"pollid\" value=\"".$pollid."\">\n";
	echo "<font class=\"content\"><b>".$question."</b></font><br><br>\n";
	echo "<table border=\"0\" cellpadding=\"2\" cellspacing=\"0\" width=\"100%\">\n";
    for($i=0;$i <= ($options-1);$i++) {
		if(isset($options)) $options = $options."<br>";
		echo "<tr><td valign=\"top\"><input type=\"radio\" name=\"option_id\" value=\"$i\" class=\"content\"> $option[$i]</td></tr>\n";
	}
	echo "<input type=\"hidden\" name=\"op\" value=\"pollvote\">\n";
	echo "</table><br><center><font class=\"content\"><input type=\"submit\" value=\""._VOTE."\"></font><br>\n";
	echo "<br><font class=\"content\"><a href=\"modules.php?name=".$module_name."&amp;op=pollvote&amp;pollid=$pollid\"><b>"._RESULTS."</b></a><br><a href=\"modules.php?name=".$module_name."\"><b>"._POLLS."</b></a><br>\n";
	if (($totalcomm !=0) || ($acomm !=0)) {
		echo "<br>"._VOTES.": <b>".$totalvotes."</b> <br> "._PCOMMENTS." <b>".$totalcomm."</b>\n\n";
	} else {
		echo "<br>"._VOTES." <b>".$totalvotes."</b>\n\n";
	}
	echo "</font></center></form>\n\n";
	CloseTable();
	include('footer.php');	
}

//Giao dien mac dinh -List all polls
function Pollmain(){
	global  $db, $prefix, $adminfile, $adminfold, $multilingual, $currentlang, $module_name;
	include('header.php');
	if ($multilingual == 1) { $querylang = "WHERE planguage='$currentlang'"; } else { $querylang = ""; }
	$list_polls = $db ->sql_query("SELECT pollid, question, totalvotes, totalcomm, time FROM ".$prefix."_nvvotings $querylang ORDER BY time DESC");
	if ($db -> sql_numrows($list_polls) > 0) {
		OpenTable();
		echo "<center><font class=\"title\"><b>"._PASTSURVEYS."</b></font></center>\n";
		CloseTable();
		echo "<br>";
		OpenTable();
		echo "<table border=\"0\" cellpadding=\"2\" cellspacing=\"0\" width=\"100%\">\n";
		while ($row = $db->sql_fetchrow($list_polls)) {
		$pollid = intval($row['pollid']);
		$question = stripslashes(FixQuotes($row['question']));
		$totalvotes = intval($row['totalvotes']);
		$totalcomm = intval($row['totalcomm']);
		$time = viewtime(intval($row['time']),1);
		$editlink ="<a href=\"".$adminfold."/".$adminfile.".php?op=poll_edit&pollid=$pollid\">"._EDIT."</a>";
		$dellink ="<a href=\"".$adminfold."/".$adminfile.".php?op=poll_del&pollid=$pollid\">"._DELETE."</a>";
		echo "<tr valign=\"top\" width=\"100%\">\n";
		echo "<td><img border=\"0\" src=\"images/modules/$module_name/icon_poll.gif\" width=\"12\" height=\"10\" title=\"$question\"></td>\n";
		echo "<td width=\"50%\"><a href=\"modules.php?name=$module_name&op=viewpoll&pollid=$pollid\"><b>".$question."</b></a>\n";
		echo "<br>".$time."</td>\n";
		echo "<td width=\"15%\"><b>".$totalvotes."</b> "._VOTES."</td>\n";
		echo "<td width=\"15%\"><b>".$totalcomm."</b> "._COMMENTS."</td>\n";
		echo "<td width=\"10%\"><a href=\"modules.php?name=$module_name&op=pollvote&pollid=$pollid\" title=\""._MORE."\">"._MORE."</a></td>\n";
		if(defined('IS_ADMMOD')){
			echo "<td>[".$editlink."-".$dellink."]</td>\n";
		}
		echo "</tr>";   
       
	}//end while loop
	echo "</table>\n";  
	CloseTable();
}else{ 
 title (_CHUACOTHAMDO);
}  
include('footer.php');
}

switch($op) {
  
    case "pollvote":
    pollvote();
    break;
   
    case "viewpoll":
    viewpoll();
    break;
 
    default:
    Pollmain();
    break;

}


?>
